<template>
  <div class="home-category">
    <!-- 左侧的 分类 -->
    <ul class="menu">
      <li
        v-for="(item, index) in list"
        :key="item.id"
        @mouseenter="activeIndex = index"
      >
        <a href="">{{ item.name }}</a>
        <template v-if="item.children && item.children.length !== 0">
          <a v-for="sub in item.children" :key="sub.id" href="#">
            {{ sub.name }}
          </a>
        </template>
        <template v-else>
          <XtxSkeleton
            width="60px"
            height="20px"
            animated
            bg="#727070"
            style="margin-right:5px"
          ></XtxSkeleton>
          <XtxSkeleton
            width="60px"
            height="20px"
            animated
            bg="#727070"
          ></XtxSkeleton>
        </template>
      </li>
    </ul>
    <!-- 鼠标悬停左侧 分类之后 出现的 弹层 -->
    <div class="layer">
      <h4>
        {{ list[activeIndex].id === 'branch' ? '品牌推荐' : '分类推荐' }}
        <small>根据您的购买或浏览记录推荐</small>
      </h4>
      <ul>
        <li v-for="it in list[activeIndex].goods" :key="it.id">
          <a href="#">
            <img :src="it.picture" alt="" />
            <div class="info">
              <p class="name ellipsis-2">{{ it.name }}</p>
              <p class="desc ellipsis">{{ it.desc }}</p>
              <p v-show="list[activeIndex].id !== 'branch'" class="price">
                <i>¥</i>{{ it.price }}
              </p>
            </div>
          </a>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { findBrandGoods } from '@/api/home'
// 导入计算属性
import { computed, ref, reactive } from 'vue'
// 导入 store函数
import { useStore } from 'vuex'
export default {
  name: 'HomeCategory',
  setup () {
    const store = useStore()
    // 当前悬停的分类下标
    const activeIndex = ref(0)
    const brandObj = reactive({
      id: 'branch',
      name: '品牌',
      children: []
    })
    const list = computed(v => {
      const _list = store.state.category.cateList.map(i => {
        return {
          name: i.name,
          id: i.id,
          goods: i.goods,
          children: i.children && i.children.slice(0, 2)
        }
      })
      _list.push(brandObj)
      return _list
    })

    findBrandGoods(6).then(res => {
      brandObj.goods = res.result
    })
    return {
      list,
      activeIndex,
      brandObj
    }
  },
  watch: {
    // 当list 获取到数据后再添加品牌的数据
    list () {
      // 在组合api里面return的数据可以通过this拿到
      this.brandObj.children.push({
        id: 2,
        name: '品牌推荐'
      })
    }
  }
}
</script>

<style scoped lang="less">
.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  z-index: 99;
  .menu {
    li {
      padding-left: 40px;
      height: 50px;
      line-height: 50px;
      &:hover {
        background: @xtxColor;
      }
      a {
        margin-right: 4px;
        color: #fff;
        &:first-child {
          font-size: 16px;
        }
      }
    }
  }
  .layer {
    width: 990px;
    height: 500px;
    background: rgba(255, 255, 255, 0.8);
    position: absolute;
    left: 250px;
    top: 0;
    display: none;
    padding: 0 15px;
    h4 {
      font-size: 20px;
      font-weight: normal;
      line-height: 80px;
      small {
        font-size: 16px;
        color: #666;
      }
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        width: 310px;
        height: 120px;
        margin-right: 15px;
        margin-bottom: 15px;
        border: 1px solid #eee;
        border-radius: 4px;
        background: #fff;
        &:nth-child(3n) {
          margin-right: 0;
        }
        a {
          display: flex;
          width: 100%;
          height: 100%;
          align-items: center;
          padding: 10px;
          &:hover {
            background: #e3f9f4;
          }
          img {
            width: 95px;
            height: 95px;
          }
          .info {
            padding-left: 10px;
            line-height: 24px;
            width: 190px;
            .name {
              font-size: 16px;
              color: #666;
            }
            .desc {
              color: #999;
            }
            .price {
              font-size: 22px;
              color: @priceColor;
              i {
                font-size: 16px;
              }
            }
          }
        }
      }
    }
  }
  &:hover {
    .layer {
      display: block;
    }
  }
}
</style>
